using UnityEngine;
using UnityEngine.Rendering;
using UnityEngine.Rendering.Universal;

public class StencilVisualizerPass : ScriptableRenderPass
{
    private RenderTargetIdentifier source;
    private RenderTextureDescriptor descriptor;
    private Material stencilMaterial;
    private RenderTargetHandle tempTexture;

    public StencilVisualizerPass(Material material)
    {
        stencilMaterial = material;
        tempTexture.Init("_TemporaryStencilTexture");
    }

    public void Setup(RenderTargetIdentifier sourceIdentifier)
    {
        source = sourceIdentifier;
    }

    public override void Configure(CommandBuffer cmd, RenderTextureDescriptor cameraTextureDescriptor)
    {
        descriptor = cameraTextureDescriptor;
        cmd.GetTemporaryRT(tempTexture.id, descriptor, FilterMode.Point);
    }

    public override void Execute(ScriptableRenderContext context, ref RenderingData renderingData)
    {
        CommandBuffer cmd = CommandBufferPool.Get("Stencil Visualizer");

        // Here you would copy the stencil buffer to tempTexture
        cmd.Blit(source, tempTexture.Identifier(), stencilMaterial);

        // Then draw it full screen
        cmd.Blit(tempTexture.Identifier(), source);

        context.ExecuteCommandBuffer(cmd);
        CommandBufferPool.Release(cmd);
    }

    public override void FrameCleanup(CommandBuffer cmd)
    {
        cmd.ReleaseTemporaryRT(tempTexture.id);
    }
}